#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.12
#####

#####-----#####-----#####-----
# NOTE:
# Changes to either R or lfe (or lfe's dependencies) since the original fitting in July 2019
# mean that the original code no longer exactly replicates the clustered standard errors
# as reported in the SI. The differences are small and do not affect any substantive interpretations.
# It is hard to say what exactly is causing this, although it may be the dof used. It is important
# to note that the coefficient estimates and number of observations still exactly match.
#####-----#####-----#####-----

library(here)
library(data.table)
library(lfe)
library(texreg)

# load the behavioral data if not already in workspace
if(!exists("behav")){
  behav <- fread(here("data", "behav.csv"))
}

set(behav, NULL, "uni_degree", as.integer(behav$edu2 %in% c("4 year degree", "Masters degree", "Professional Degree (JD, MD)", "Doctorate")))
set(behav, NULL, "not_white", as.integer(behav$race2 != "White/Caucasian"))
set(behav, NULL, "news_daily", as.integer(behav$news2 == 7))
set(behav, NULL, "younger", as.integer(behav$age < 42))

behav[ , pid2_het := factor(
  # this approach uses a lookup table
  c("Democrat" = "Democrat", "Independent" = "Independent", "Something else" = "Independent", "Republican" = "Republican")[as.character(pid2)],
  levels = c("Independent", "Democrat", "Republican")
  )]

# Fitting -----------------------------------------------------------------

did_gender <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:female + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:female + foreign_partner:CH:female + foreign_partner:CS:female | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_gender)

did_uni <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:uni_degree + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:uni_degree + foreign_partner:CH:uni_degree + foreign_partner:CS:uni_degree | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_uni)

did_age <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:younger + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:younger + foreign_partner:CH:younger + foreign_partner:CS:younger | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_age)

did_race <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:not_white + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:not_white + foreign_partner:CH:not_white + foreign_partner:CS:not_white | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_race)

did_news2 <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:news_daily + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:news_daily + foreign_partner:CH:news_daily + foreign_partner:CS:news_daily | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_news2)

did_pid <- felm(tokens ~ female_partner + foreign_partner + foreign_partner:pid2_het + foreign_partner:NM + foreign_partner:CH + foreign_partner:CS + foreign_partner:NM:pid2_het + foreign_partner:CH:pid2_het + foreign_partner:CS:pid2_het | game_NR + ResponseId | 0 | ResponseId, data = behav)
summary(did_pid)


# Reporting ---------------------------------------------------------------

texreg(
  list(
    did_gender,
    did_age,
    did_pid,
    did_uni,
    did_race,
    did_news2
  ),
  custom.coef.map = list(
    "female_partner" = "Female Partner",
    "foreign_partner" = "Foreign-Born Partner",
    
    "foreign_partner:femaleTRUE" = "Fgn. Ptnr. $\\times$ Female",
    "foreign_partner:younger" = "Fgn. Ptnr. $\\times$ Age $<$ 42",
    "foreign_partner:pid2_hetDemocrat" = "Fgn. Ptnr. $\\times$ Democrat",
    "foreign_partner:pid2_hetRepublican" = "Fgn. Ptnr. $\\times$ Republican",
    "foreign_partner:uni_degree" = "Fgn. Ptnr. $\\times$ Uni. Degree",
    "foreign_partner:not_white" = "Fgn. Ptnr. $\\times$ Not White",
    "foreign_partner:news_daily" = "Fgn. Ptnr. $\\times$ News: Daily",
    
    "foreign_partner:NM" = "Fgn. Ptnr. $\\times$ NM.",
    "foreign_partner:CS" = "Fgn. Ptnr.$\\times$ C.S.",
    "foreign_partner:CH" = "Fgn. Ptnr. $\\times$ C.H.",
    
    "foreign_partner:femaleTRUE:NM" = "Fgn. Ptnr. $\\times$ Female $\\times$ NM.",
    "foreign_partner:femaleTRUE:CS" = "Fgn. Ptnr. $\\times$ Female $\\times$ C.S.",
    "foreign_partner:femaleTRUE:CH" = "Fgn. Ptnr. $\\times$ Female $\\times$ C.H.",
    
    "foreign_partner:younger:NM" = "Fgn. Ptnr. $\\times$ Age $<$ 42 $\\times$ NM.",
    "foreign_partner:younger:CS" = "Fgn. Ptnr. $\\times$ Age $<$ 42 $\\times$ C.S.",
    "foreign_partner:younger:CH" = "Fgn. Ptnr. $\\times$ Age $<$ 42 $\\times$ C.H.",
    
    "foreign_partner:pid2_hetDemocrat:NM" = "Fgn. Ptnr. $\\times$ Democrat $\\times$ NM.",
    "foreign_partner:pid2_hetDemocrat:CS" = "Fgn. Ptnr. $\\times$ Democrat $\\times$ C.S.",
    "foreign_partner:pid2_hetDemocrat:CH" = "Fgn. Ptnr. $\\times$ Democrat $\\times$ C.H.",
    
    "foreign_partner:pid2_hetRepublican:NM" = "Fgn. Ptnr. $\\times$ Republican $\\times$ NM.",
    "foreign_partner:pid2_hetRepublican:CS" = "Fgn. Ptnr. $\\times$ Republican $\\times$ C.S.",
    "foreign_partner:pid2_hetRepublican:CH" = "Fgn. Ptnr. $\\times$ Republican $\\times$ C.H.",
    
    "foreign_partner:uni_degree:NM" = "Fgn. Ptnr. $\\times$ Uni. Degree $\\times$ NM.",
    "foreign_partner:uni_degree:CS" = "Fgn. Ptnr. $\\times$ Uni. Degree $\\times$ C.S.",
    "foreign_partner:uni_degree:CH" = "Fgn. Ptnr. $\\times$ Uni. Degree $\\times$ C.H.",
    
    "foreign_partner:not_white:NM" = "Fgn. Ptnr. $\\times$ Not White $\\times$ NM.",
    "foreign_partner:not_white:CS" = "Fgn. Ptnr. $\\times$ Not White $\\times$ C.S.",
    "foreign_partner:not_white:CH" = "Fgn. Ptnr. $\\times$ Not White $\\times$ C.H.",
    
    "foreign_partner:news_daily:NM" = "Fgn. Ptnr. $\\times$ News: Daily $\\times$ NM.",
    "foreign_partner:news_daily:CS" = "Fgn. Ptnr. $\\times$ News: Daily $\\times$ C.S.",
    "foreign_partner:news_daily:CH" = "Fgn. Ptnr. $\\times$ News: Daily $\\times$ C.H."
  ),
  dcolumn = T,
  booktabs = T,
  include.adjrs = F,
  include.rsquared = F,
  include.rmse = F,
  caption = "Exploratory Heterogeneity Analysis for \\textit{Tokens Given}",
  caption.above = T,
  label = "tab:subgroupsDID",
  fontsize = "scriptsize",
  stars = c(0.01, 0.05, 0.10),
  digits = 3,
  custom.model.names = paste0("(",1:6,")"),
  custom.note = "",
  center = F
)
